<?php

require('init.php');

// Download testcase
if( isset($_GET['fetch']) &&  isset($_GET['rank']) )
{
	if( !empty($_GET['fetch']) && !empty($_GET['rank']) )
	{
		$rank= $_GET['rank'];
		$probid = $_GET['probid'];
		$filename = $probid . $rank . ".in";

		$query = "SELECT OCTET_LENGTH(input) AS `size` FROM `testcase` 
				WHERE `probid`='".$probid."' AND `rank`='".$rank."'";
		
		$res = mysql_query($query ,$conn_db2);	
		if($res)
		{
			$size = mysql_fetch_array($res, MYSQL_BOTH);
		}else{
			throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conn_db2) . ", query: '$query'");
		}		
		//print_r($size);		

		///*$size = $DB->q("MAYBEVALUE SELECT OCTET_LENGTH('input')
	          //     	 FROM testcase WHERE probid = %s AND rank = %i",
	            //   	$probid, $rank);

		// sanity check before we start to output headers
	if ( $size['size']===NULL || !is_numeric($size['size'])) error("Problem while fetching testcase");

		header("Content-Type: application/octet-stream; name=\"$filename\"");
		header("Content-Disposition: inline; filename=\"$filename\"");
		header("Content-Length: ".$size['size']."");

		// This may not be good enough for large testsets, but streaming them
		// directly from the database query result seems overkill to implement.
//		echo $DB->q("VALUE SELECT $fetch FROM testcase
//	             WHERE probid = %s AND rank = %i", $probid, $rank);

		$q = "SELECT `input` FROM `testcase` 
				WHERE `probid`='".$probid."' AND `rank`='".$rank."'";
		
		$r = mysql_query($q ,$conn_db2);	
		if($r)
		{
			$in = mysql_fetch_array($r, MYSQL_BOTH);
			echo($in['input']);
		}else{
			throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conn_db2) . ", query: '$q'");
		}

		exit(0);
	}
}

?>
<!--
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> -->
<!--<script src="http://code.jquery.com/jquery-latest.js"></script>-->
<script src="../jquery-latest.js"></script>
<link rel="stylesheet" type="text/css" href="../examples_start_stop.css">
<script type="text/javascript" src="../jquery.lwtCountdown-1.0.js"></script><!-- -->

<?php

$title = 'Submit';


if( !isset($_REQUEST['cid']) || !isset($_REQUEST['pid']) )
{
		header('Location: index.php');
}
if( empty($_REQUEST['cid']) || empty($_REQUEST['pid']) )
{
	header('Location: index.php');
}

require(LIBWWWDIR . '/header.php');
require(LIBWWWDIR . '/forms.php');

$probid = @$_REQUEST['pid'];

echo '<h1> Submit Source code to: '.$_REQUEST['pid'].'</h1>';

$data = get_testcasein_past($probid, $conn_db2);	
echo '<div id="capa">
</div>';
echo addForm('add_scode.php','post',null,'multipart/form-data').
'<input type="hidden" name= "prob" id="prob" value="'.$probid.'"/>'.
'<input type="hidden" name= "cid" id="cid" value="'.$_REQUEST['cid'].'"/>';

?>
<th>
<!----><div id="countdown_dashboard" >
	<div class="dash minutes_dash" >
		<span class="dash_title">minutes</span>
       	 	<div class="digit">0</div>
       	 	<div class="digit">0</div>
	</div>
	<div class="dash seconds_dash" >
		<span class="dash_title">seconds</span>
       	 	<div class="digit">0</div>
       	 	<div class="digit">0</div>
	</div>
<!----></div>
</th>
<br/><br/><br/><table class="list testcases">
<thead><tr><th scope="col">#&nbsp;</th><th scope="col">download</th>
<th scope="col">upload Out file</th></tr></thead><tbody>
<?php
for($i=1; $i<=$data[0]; $i++)
{
	echo "<tr><td rowspan=\"1\" class=\"testrank\">".$data[$i]['rank']."&nbsp;</td>".
	"<td class=\"filename\"><a class=\"show\" href=\"./websubmit_post.php?probid=".urlencode($probid).
	"&rank=".$data[$i]['rank']."&fetch=1 \">".
	htmlspecialchars($probid.$data[$i]['rank'].".in")."</a></td>" .
	"<td class=\"up_file\">" . addFileField("program_out".$data[$i]['rank']) . "</td>".	
	 "</td></tr>\n";
}
echo "</table> <br/>" . addHidden('ntest', $data[0]).
'<table><tr><td><label>Upload source code: <label>'.addFileField("source_code") .'</td></tr>
<tr><td><label for="langext">Language</label>:'.
addSelect('langext', get_languas($conn_db2), '', true).
'</td></tr></table><br/><br/><input type="hidden" name="cm" id="cm" value="2"/>'.
addSubmit('Submit all changes', 'submit') . 
addEndForm();

require(LIBWWWDIR . '/footer.php');
?>
<script language="javascript">
$(document).ready(function(){
var clik1, click2;
var link = false;
function start_time(){
	$('#countdown_dashboard').countDown({
		targetOffset: {
			'day': 		0,
			'month': 	0,
			'year': 	0,
			'hour': 	0,
			'min': 		4,
			'sec': 		0
		}});
}
function check_tinit(){
	var prob = $("#prob").val();
	var cont = $("#cid").val();
	$.ajax({
  		type: 'post',
  		url: 'time.php',
  		data: 'cm=3&prob='+prob+'&cid='+cont });
}
jQuery('#submit').hide();

$(".up_file input").click( function(event){
	click1 = true;
	if(link == false){ 
		check_tinit();
		link = true;
	}
	if(click2 == true){ jQuery('#submit').show(); }
});

$("#source_code").click(function(event){
	click2 = true;
	if(link == false){ 
		check_tinit();
		link = true;
	}
	if(click1 == true){ jQuery('#submit').show(); }
});

$(".filename a").click( function(event){ 
	start_time();
	link = true;

	$(".filename a").unbind();

	setTimeout(function(){ jQuery('#submit').hide(); },"240000");
	
	var pid = $("#prob").val();
	var cid = $("#cid").val();

	$.ajax({
  		type: 'post',
  		url: 'time.php',
  		data: 'cm=1&pb='+pid+'&cid='+cid });
	});
});
</script>

